package design.mode.model.strategy;

import design.mode.dispatcher.util.test.Test;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
public class AsyncServiceImpl implements AsyncService {
    @Override
    @Async("myLocalExecutor")
    public CompletableFuture<List<Test>> execute(List<Test> group) {
        System.out.println("当前线程名称：" + Thread.currentThread().getName());
        LinkedList<Test> localList = new LinkedList<>();

        try {
            for (int i = 0; i < group.size(); i++) {
                Test test = group.get(i);
                Test test1 = new Test();
                Thread.sleep(100);
                BeanUtils.copyProperties(test1, test);
                localList.add(test1);
            }
        } catch (Exception e) {
            // 记录异常，避免中断任务
            e.printStackTrace();
            return CompletableFuture.completedFuture(Collections.emptyList()); // 返回空列表而不是 null
        }

        return CompletableFuture.completedFuture(localList);
    }
}
